#!/bin/bash
case $1 in
start)
#*------------------------------------begin start cmd------------------------------------*
    ps -fe|grep @project.main.class@ |grep -v grep
    if [ $? -eq 0 ]
    then
        echo "@project.artifactId@已经在运行，不能重复运行！"
        exit 1
    fi
    declare -A map=(["dev"]="开发" ["test"]="测试" ["prod"]="生产")
    echo "请输入运行环境【开发:dev,测试:test,生产:prod】"
    read profile
    if [ -z "${map[$profile]}" ]
    then
        echo "输入错误"
        exit 1
    fi

    echo "将要在【${map[$profile]}】环境下运【@project.artifactId@-@project.version@】程序,请再次确认"
    read profile2

    if [ $profile2 != $profile ]
    then
        echo "两次输入不一致"
        exit 1
    fi

    echo "start pull and copy dependencies jars from nexus..."
    rm -rf lib/*.jar
    mvn dependency:copy-dependencies -DincludeScope=runtime -DoutputDirectory=lib
    rm -f lib/servlet-api-2.5.jar
    echo "=======================jars copied================================"
    echo "starting program, pls wait for 5 seconds ...."

    echo active-profile is: $profile
    java -classpath .:lib/* @project.jvm.options@ @project.main.class@ --spring.profiles.active=$profile --server.port=@server.port@ > /dev/null 2>&1 &
    sleep 5
    tail -f @logs.store.path@/@project.artifactId@.log
#*------------------------------------end start cmd------------------------------------*
    ;;
stop)
    echo "stop..."
    curl -X POST http://127.0.0.1:@server.port@/shutdown
    tail -f @logs.store.path@/@project.artifactId@.log
    ;;
restart)
    echo "restart..."
    curl -X POST http://127.0.0.1:@server.port@/restart
    tail -f @logs.store.path@/@project.artifactId@.log
    ;;
pause)
    echo "pause..."
    curl -X POST http://127.0.0.1:@server.port@/pause
    tail -f @logs.store.path@/@project.artifactId@.log
    ;;
resume)
    echo "resume..."
    curl -X POST http://127.0.0.1:@server.port@/resume
    tail -f @logs.store.path@/@project.artifactId@.log
    ;;
version)
    echo "============================================="
    echo "name:@project.name@"
    echo "port:@server.port@"
    echo "description:@project.description@"
    echo "version:@project.version@"
    echo "time:@timestamp@"
    echo "============================================="
    ;;
clear)
    rm -rf lib/*.jar
    rm -rf /usr/local/apache-maven-3.3.9/repository/com/danbay/cloud/
    rm -rf /usr/local/apache-maven-3.3.9/repository/com/danbay/danbayframework/
    echo "clear done."
    ;;
*)
    echo "参数错误，可用的参数有"
    echo "=============================="
    echo "start:启动程序"
    echo "stop:停止程序"
    echo "restart:重启程序"
    echo "pause:暂停程序"
    echo "resume:继续暂停的程序"
    echo "version:查看程序版本信息"
    echo "clear:清除业务组件jar包缓存"
    echo "=============================="
    exit 1
    ;;
esac



